package com.teaRestaurant.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaRestaurant.domain.DTO.CategoryPageQueryDTO;
import com.teaRestaurant.domain.entity.Category;
import com.teaRestaurant.mapper.CategoryMapper;
import com.teaRestaurant.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public IPage<Category> list(CategoryPageQueryDTO categoryPageQueryDTO) {
        System.out.println(categoryPageQueryDTO);
        int pageNum = categoryPageQueryDTO.getPageNum();
        int pageSize = categoryPageQueryDTO.getPageSize();
        IPage<Category> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Category::getName,categoryPageQueryDTO.getName());
        if (categoryPageQueryDTO.getType() != null)
        wrapper.eq(Category::getType, categoryPageQueryDTO.getType());
        IPage<Category> categoryIPage = categoryMapper.selectPage(page, wrapper);
        IPage<Category> categoryIPage2 = new Page<>();
        BeanUtils.copyProperties(categoryIPage,categoryIPage2);
        System.out.println(categoryIPage2);
        return categoryIPage2;
    }

    @Override
    public void insert(Category category) {
        category.setOperateTime(LocalDateTime.now());
        categoryMapper.insert(category);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            categoryMapper.deleteById(id);
        }
    }

    @Override
    public void updateStatus(Long id) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(category.getStatus() == 1?2:1);
        category.setOperateTime(LocalDateTime.now());
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getId,category.getId());
        categoryMapper.update(category,wrapper);
    }

    @Override
    public List<Category> allList(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getType, type);
        return categoryMapper.selectList(wrapper);
    }
}
